//
//  FriendImpressionViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/8.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "FriendImpressionViewController.h"
#import "FriendImpressionTableViewCell.h"
#import "FriendImpressionCell.h"
#import "InServerCompangyVC.h"
@interface FriendImpressionViewController ()
<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * dataSouce;
    int totalW;
}
@end

@implementation FriendImpressionViewController
#pragma mark - 视图的生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    
//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app.viewController isHiddenCustomTabBarByBoolean:YES];
//    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    [self addBackItem];
    if (_isNeedDescription == YES) {
        self.navigationItem.title = @"其他人对TA的印象";
        [self addRightItem];
    }
    else
    {
        self.navigationItem.title = @"好友印象";
    }
  
    // Do any additional setup after loading the view.
}
-(void)main
{
    dataSouce = [[NSMutableArray alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT-63) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"FriendImpressionTableViewCell" bundle:nil] forCellReuseIdentifier:@"FriendImpressionTableViewCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"FriendImpressionCell" bundle:nil] forCellReuseIdentifier:@"FriendImpressionCell"];
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self requestOtherData];
}
-(void)requestOtherData
{
    NSString * str = nil;
    if (_isNeedDescription == YES) {
        str = _friendCode;
    }
    else
    {
        str = [Singleton share].code;
    }
    
    
    [U76HONet friendImpressionSecWithacton:@"" code:str andDataBlock:^(NSArray *arr) {
        NSLog(@"好友对我的印象数据获取成功了");
        [dataSouce addObjectsFromArray:arr];
        [_tableView reloadData];
    }];

}
#pragma mark - _tabelViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSouce.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FriendImpressionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FriendImpressionTableViewCell"];
        cell.dataArr = dataSouce;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        FriendImpressionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"FriendImpressionCell"];
        if (dataSouce.count!=0) {
            cell.model = dataSouce[indexPath.row-1];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        int j = 0;
        totalW = 0;
        NSMutableArray * mArr = [[NSMutableArray alloc]init];
        for (int i = 0; i<dataSouce.count; i++) {
            FirendImpretionModel * model = dataSouce[i];
            [mArr addObject:model.tags];
        }
        //    CGFloat h = (self.frame.size.height - SIZETO6H(16)*2-SIZETO6H(10))/2;
        CGFloat h = 30;
        CGFloat myWidth = SCREEN_WIDTH - SIZETO6W(30);
        for (int i = 0; i<mArr.count; i++) {
            NSString * str = [mArr objectAtIndex:i];
            CGFloat tempW = [str widthOfSize:18] + SIZETO6W(10);
            totalW += tempW+SIZETO6W(10);
            if (totalW<=myWidth) {
                
            }
            else
            {
                totalW = tempW+SIZETO6W(10);
                j = j+1;
                
            }
        }
        return SIZETO6H(15)+20+j*(h + SIZETO6H(10)) + 30+20;

    }
    else
    {
        return 44;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isNeedDescription == NO)
    {
        if (indexPath.row != 0) {
           FirendImpretionModel * mod = dataSouce[indexPath.row-1];
            InServerCompangyVC * inscVC = [[InServerCompangyVC alloc]init];
            inscVC.isMydyna = @"mine";
            inscVC.code = mod.code;
            [self.navigationController pushViewController:inscVC animated:YES];
        }
    }
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    if (_isAPNS) {
        AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        self.view.window.rootViewController = app.viewController;
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)addRightItem
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 100, 30);
    [btn setTitle:@"对TA描述" forState:UIControlStateNormal];
    [btn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
-(void)rightBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
